# -*- coding: utf-8 -*-
# @Time    : 2022/11/17 10:21 
# @Author  : gu'leng
# @File    : yaml_util.py
# @Software: PyCharm
import os
from config import BASE_DIR
import yaml


class YamlUtil:
    # 读取extract.yml文件

    def read_extract_yaml(self, key, datadir='data'):
        filepath = os.path.join(BASE_DIR, datadir, "extract.yml")
        with open(filepath, mode='r', encoding='utf-8') as f:
            value = yaml.load(stream=f, Loader=yaml.FullLoader)
            return value[key]

    # 写入extract.yml文件
    def write_extract_yaml(self, data, datadir='data'):
        filepath = os.path.join(BASE_DIR, datadir, "extract.yml")
        with open(filepath, mode='a+', encoding='utf-8') as f:
            value = yaml.dump(data=data, stream=f, allow_unicode=True)
            return value

    # 清除extract_yml文件
    def clear_extract_yaml(self, datadir="data"):
        filepath = os.path.join(BASE_DIR, datadir, "extract.yml")
        with open(filepath, mode='w', encoding="utf-8") as f:
            f.truncate()

    # 读取测试用例.yml文件
    # def read_testcase_yaml(self, yaml_name):
    #     with open(os.getcwd() + "\\a.yml", mode='r', encoding='utf-8') as f:
    #         value = yaml.load(stream=f, Loader=yaml.FullLoader)
    #         return value
